/*
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-03-09 16:40:45
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-03-19 21:07:06
 * @FilePath: /free-learning/src/api/system/school/index.ts
 */
import request from "@/utils/request";
import type { SchoolPageQueryParams, SchoolType } from "./type";

const SCHOOL_BASE_URL = "/api/v1/school";

/** 学校选项 */
export interface SchoolOption {
  /** 学校ID */
  id: number;
  /** 学校名称 */
  name: string;
}

/**
 * 分页查询学校
 * @param params 查询参数
 */
export const pageQuery = (params: Partial<SchoolPageQueryParams>) => {
  return request<any, PageResult<SchoolType>>({
    url: `${SCHOOL_BASE_URL}/page`,
    method: "post",
    data: params,
  });
};

/**
 * 获取所有学校
 */
export const getAllSchool = () => {
  return request<any, SchoolType[]>({
    url: `${SCHOOL_BASE_URL}`,
    method: "get",
  });
};

/**
 * 根据id获取学校
 * @param id 学校id
 */
export const getSchoolById = (id: string) => {
  return request<any, SchoolType>({
    url: `${SCHOOL_BASE_URL}/${id}`,
    method: "get",
  });
};

/**
 * 保存或更新学校
 * @param params 学校信息
 */
export const saveAndUpdateSchool = (params: Partial<SchoolType>) => {
  return request<any, boolean>({
    url: `${SCHOOL_BASE_URL}${params.id ? `/${params.id}` : ""}`,
    method: params.id ? "put" : "post",
    data: params,
  });
};

/**
 * 根据id删除学校
 * @param id 学校id
 */
export const deleteSchoolById = (id: string) => {
  return request<any, boolean>({
    url: `${SCHOOL_BASE_URL}/${id}`,
    method: "delete",
  });
};
/**
 * 获取所有学校列表
 *
 * @param keywords 搜索关键词
 * @returns 学校列表
 */
export const getAllSchools = (keywords: string = "") => {
  return request<any, SchoolOption[]>({
    url: `${SCHOOL_BASE_URL}/all`,
    method: "post",
    data: { keywords },
  });
};
